/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.example;

import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.container.XNameAccess;
import com.sun.star.deployment.PackageInformationProvider;
import com.sun.star.deployment.XPackageInformationProvider;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sdb.XBookmarksSupplier;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XURLTransformer;
import java.awt.Color;
import java.awt.Container;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Shameera
 */
public class TestClass {

    private final XComponentContext m_xContext;
    private String message = "";
    private String loadFile;
    private String pathToTheOXT;
    private XComponentContext xRemoteContext;
    private XMultiComponentFactory xRemoteServiceManager;

    public TestClass(XComponentContext m_xContext) throws IOException, Exception {
        this.m_xContext = m_xContext;
        pathToTheOXT = getPathToOXT();
        //showJTree();
        // new FolderViwer(pathToTheOXT);
                    myMain("./Components/subFolder1");
//        printOnConsole();
    }

    private String getPathToOXT() {
        try {
            XPackageInformationProvider xPackageInformationProvider = PackageInformationProvider.get(m_xContext);
            String location = xPackageInformationProvider.getPackageLocation("com.example.OOTest_1");
            Object oTransformer = m_xContext.getServiceManager().createInstanceWithContext("com.sun.star.util.URLTransformer", m_xContext);
            XURLTransformer xTransformer = (XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, oTransformer);
            com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1];
            oURL[0] = new com.sun.star.util.URL();
            oURL[0].Complete = location + "/Components";
            xTransformer.parseStrict(oURL);
            String path = oURL[0].Path + oURL[0].Name;
    //        System.out.println("in gerPathToTheOXT :"+path);
            return path;
//            return new File(defultPath).list();
        } catch (com.sun.star.uno.Exception ex) {
            System.out.println(ex);
            System.out.println(ex.getStackTrace());
        }

        return null;
    }

    private String[] getFileList(String path) {
        return new File(path).list();
    }
//    private void printFileNsmes(){
//        XMultiComponentFactory xmcf = this.m_xContext.getServiceManager();
//        try {
//            Object desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
//            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface
//                            (com.sun.star.frame.XDesktop.class, desktop);
//            XComponent xDoc = xDesktop.getCurrentComponent();
//             XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(
//                XTextDocument.class, xDoc);
//             XText xText = xTextDocument.getText();
//             String [] strings = getFileList();
//             for(String s : strings){
//                  xText.setString("\n "+ s);
//             }
//
//        } catch (Exception ex) {
//            Logger.getLogger(OOTest_1.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    private void printOnConsole() throws IOException, Exception {
        if (getFileList(pathToTheOXT) != null) {
            String[] strings = getFileList(pathToTheOXT);
            for (String s : strings) {
                System.out.println(s);
                if (s.equalsIgnoreCase("CEP_Template.ott")) { //   Template-2.odt
                    loadFile = pathToTheOXT + "/" + s;
                }
                message += " " + s;
            }
            System.out.println(message);
            System.out.println("path name is : " + loadFile);
           // loadTemplate();
//            ZipReader zr = new ZipReader(new PathConverter().getWindowsPath(loadFile));
            //JOptionPane.showMessageDialog(null, message);
        } else {
            JOptionPane.showMessageDialog(null, "getFileList is not working so sad");
        }
    }

    private void loadTemplate() throws Exception {
        if (loadFile != null) {
            //XMultiComponentFactory xmcf = m_xContext.getServiceManager();
            try {
                // get the remote office component context
                xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
                System.out.println("Connected to a running office ...");
                xRemoteServiceManager = xRemoteContext.getServiceManager();
            } catch (BootstrapException ex) {
                Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
            }
            Object oDesktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
            XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
            PropertyValue[] loadprop = new PropertyValue[1];
            PropertyValue asTemplate = new PropertyValue();
            asTemplate.Name = "AsTemplate";
            asTemplate.Value = new Boolean(true);
            loadprop[0] = asTemplate;
            XComponent xTextComponent = xComponentLoader.loadComponentFromURL("file://" + loadFile, "_blank", 0, loadprop);
        }
    }

    // this is a working main part of the requierment 4
    public void showJTree() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SimpleNavigator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SimpleNavigator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SimpleNavigator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SimpleNavigator.class.getName()).log(Level.SEVERE, null, ex);
        }
        SimpleNavigator simpleNavigator = new SimpleNavigator();
        simpleNavigator.showJTree(pathToTheOXT);
        simpleNavigator.setVisible(true);
    }

     public void myMain(String dirPath){
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MyFileTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MyFileTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MyFileTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MyFileTree.class.getName()).log(Level.SEVERE, null, ex);
        }
    JFrame frame = new JFrame("FileTree");
    frame.setForeground(Color.black);
    frame.setBackground(Color.lightGray);
    Container cp = frame.getContentPane();

//    if (av.length == 0) {
      cp.add(new MyFileTree(new File(dirPath)));
//    } else {
//      cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
//      for (int i = 0; i < av.length; i++)
//        cp.add(new MyFileTree(new File(av[i])));
//    }

    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }

}
